using System;
using System.Data.Sql;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;



    public partial class Results : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //list for the command button event
                this.GridView1.RowCommand+=new GridViewCommandEventHandler(GridView1_RowCommand);
                
            }            
        }


        public void Populate()
        {
            try
            {
                //configure the sql datasource with the correct input parameters
                if (Session["LoadTest"] != null)
                {
                    string project = (string)Session["LoadTest"];
                    int pastDays = (int)Session["pastDays"];
                    if (pastDays < 0) pastDays = int.MaxValue;

                    this.SqlDataSource1.SelectParameters.Clear();
                    this.SqlDataSource1.SelectParameters.Add("LoadTestName", project);

                    Parameter p = new Parameter("PastDays", TypeCode.Int32, pastDays.ToString());
                    this.SqlDataSource1.SelectParameters.Add(p);
                }

                //set the correct page
                if (Session["CurrentPageIndex"] != null)
                {
                    this.GridView1.PageIndex = (int)Session["CurrentPageIndex"];
                }
                this.GridView1.DataBind();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
            
        }

       
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                int index = Convert.ToInt32(e.CommandArgument);

                //get the runid for the index clicked
                int runID = (int)GridView1.DataKeys[index].Value;


                switch (e.CommandName)
                {
                    case "Results":
                        string reportServer = System.Web.Configuration.WebConfigurationManager.AppSettings["ReportServer"];
                        Response.Redirect(reportServer+"/?%2fLoadTestReports%2fLoadTestSummary&RunId=" + runID, false);
                        break;
                    case "Edit":
                        Session["RunId"] = runID.ToString();
                        Response.Redirect("Default.aspx", false);
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to delete this load test?')");

            }


        }
        
}
    




